﻿using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Castle.Core.Logging;
using FwProject.Comm;
using FwProject.ShoppingMall.EventAdvertising;
using FwProject.ShoppingMall.Shop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FwProject.RemoteEventBus
{

    [RemoteEventHandler(ForType = RemoteTypeHelp.TypeShopSecondkill, ForTopic = RemoteTypeHelp.TOPShopSecondkill)]
    public class RemoteEventBusShopScendKill : IRemoteEventHandler, ITransientDependency
    {
        private readonly IRepository<ShopSecondkill, Guid> _shopSecondkillRepository;
        private readonly IRepository<ShopProduct, Guid> _shopProductRepository;

        public ILogger iLogger { get; set; }
        public  RemoteEventBusShopScendKill(IRepository<ShopSecondkill, Guid> shopSecondkillRepository,
            IRepository<ShopProduct, Guid> shopProductRepository) {
            _shopSecondkillRepository = shopSecondkillRepository;
            _shopProductRepository = shopProductRepository;
        }
        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {
            try
            {
                var model = _shopSecondkillRepository.GetAll();
                if (model != null)
                {
                    foreach (var item in model)
                    {
                        if (item.SecondkillEndTime <= DateTime.Now)
                        {
                            _shopSecondkillRepository.Delete(item);
                            var m = _shopProductRepository.Get(item.ShopProductId);
                            m.ShopProductFlag = ShopProductFlag.正常;
                        }
                    }
                  
                }
            }
            catch (Exception exe)
            {

                iLogger.Error("秒杀商品修改失败", exe);
            }
            finally {

                iLogger.Info("秒杀商品到期修改");
            }
          
        }
    }
}
